package com.feasy.generator.managedb.service;

import com.feasy.generator.managedb.utils.DataBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Service
public class ConnectDbServiceImpl implements ConnectDbService{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public boolean dbConnectTest(String dbtype, String dbUrl, String username, String password) throws SQLException {
        Connection connection = DataBaseUtils.getConnection(dbtype, dbUrl, username, password);
        if (connection == null) {
            return false;
        }
        connection.close();
        return true;
    }

    @Override
    public List<Map<String, Object>> getDbTableList(String dbtype, String jdbcurl, String username, String password) {
        List<Map<String, Object>> list = null;
        Connection connection = DataBaseUtils.getConnection(dbtype, jdbcurl, username, password);
        if ("1".equals(dbtype)) {
            String sql = DataBaseUtils.getTableNameListSql();
            list = DataBaseUtils.getTableNameList(connection);
        } else if ("2".equals(dbtype)) {
            list = DataBaseUtils.getTableNameListOracle(connection, username);
        }
        return list;
    }

}
